package com.nhjf.common.util.tools;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.nhjf.common.util.tools.LogMessage;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class DataConvertor {

	public static Integer parseInteger(String str) {
		if (str == null || str.trim().equals(""))
			return null;
		return Integer.valueOf(str);
	}

	public static int parseInt(String str) {
		if (str == null || str.trim().equals(""))
			return 0;
		return Integer.parseInt(str);
	}

	public static int parseInt(Integer str) {
		if (str == null)
			return 0;
		return str.intValue();
	}

	public static Double parseDouble(String str) {
		if (str == null || str.trim().equals(""))
			return null;
		return Double.valueOf(str);
	}

	public static double parseDoubleValue(String str) {
		if (str == null || str.trim().equals(""))
			return 0;
		return Double.valueOf(str).doubleValue();
	}

	public static Date parseDate(String str, String format) {
		if (str == null || str.trim().equals(""))
			return null;
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			Date date = simpleDateFormat.parse(str);
			return date;
		} catch (Exception e) {
			return null;
		}
	}

	public static Timestamp parseTimestamp(String str) {
		if (str == null || str.trim().equals(""))
			return null;
		return Timestamp.valueOf(str);
	}

	/**
	 * 把yyyy-MM-dd格式的字符串转换成Date
	 * 
	 * @param dateStr
	 * @return Timestamp
	 */
	public final static Date str2Date(String dateStr) {
		if (dateStr == null || dateStr.length() == 0) {
			return null;
		}

		DateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
		Date da = null;
		try {
			da = df.parse(dateStr);
		} catch (Exception e) {
			LogMessage.logwrite(DataConvertor.class, e.getMessage());
			return null;
		}
		return da;
	}

	public static String getCurDateStr(String formatStr) {
		Date curDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(curDate);
	}

	public final static String date2Str(Date da, String pattern) {
		if (da == null) {
			return "";
		}
		SimpleDateFormat df = new java.text.SimpleDateFormat(pattern);
		return df.format(da);
	}
	
	
	public static  Date dateToEndDate(Date date){
		Date newDate=null;
		if(date!=null){
			newDate =getDate("yyyy-MM-dd HH:mm:ss",DataConvertor.date2Str(date, "yyyy-MM-dd")+" 23:59:59");
		}
		return newDate;
	}

	/**
	 * 
	 * @param Format
	 *            格式类型 :yyyy-MM-dd HH:mm:ss
	 * @param value
	 *            :2012-08-23 07:39:24
	 * @return 返回 :Thu Aug 23 07:39:24 CST 2012
	 */
	public static Date getDate(String Format, String value) {
		Date date = null;

		try {
			SimpleDateFormat sf = new SimpleDateFormat(Format);
			date = sf.parse(value);
		} catch (ParseException e) {
			LogMessage.logwrite(DataConvertor.class, e.getMessage());
		} catch (java.text.ParseException e) {
			LogMessage.logwrite(DataConvertor.class, e.getMessage());
		}
		return date;
	}
	

	// 天数操作
	public static String dayOption(int days) {
		SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_YEAR);
		calendar.set(Calendar.DAY_OF_YEAR, day + days);
		Date cc = calendar.getTime();
		return form.format(cc);
	}

	// 月数操作
	public static String MonthOption(int days) {
		SimpleDateFormat form = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, day + days);
		Date cc = calendar.getTime();
		return form.format(cc);
	}
	//获取当天以后的几天
	public static String dayAfterNowDate(int num){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, num);		
		SimpleDateFormat   sdf   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String afterDate = sdf.format(calendar.getTime());	
		return afterDate;
	}
}
